package cn.bluethink.eguan.core.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.bluethink.eguan.core.security.Token;
import cn.bluethink.eguan.core.service.EgCommentService;
import cn.bluethink.eguan.core.service.EgUserService;
import cn.bluethink.eguan.filter.OtypeFilter;
import cn.bluethink.eguan.model.core.EgComment;
import cn.bluethink.eguan.model.core.EgPurview;
import cn.bluethink.eguan.model.core.EgUser;
import cn.bluethink.eguan.result.PageInfo;
import cn.bluethink.eguan.result.ResponseMessage;
import cn.bluethink.eguan.result.ResponseResult;

/**
 * eguan+评论控制器
 * 
 * @title 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.1.0
 * @describe
 * @author liuxn
 * @date 2018年10月24日
 * @note 修订历史： （1）liuxn于2018年10月24日构建初始版本0.1 
 */
@CrossOrigin
@RestController
@RequestMapping("/eguan/v1.0/comment")
public class EgCommentController {

	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private EgCommentService commentService;
	
	@Autowired
	private EgUserService userService;
	
	/**
	 * 发表评论
	 * @param comment
	 * @return
	 * @throws Exception
	 */
	@Token
	@PostMapping("/create")
	public ResponseResult<EgComment> create(@RequestBody EgComment comment) throws Exception {

		// 1、获取当前登录用户Id
		//String token = request.getHeader("Authorization");
		Long uid = (Long)request.getAttribute("userId");
		// 2、判断参数是否合法
		if(!comment.isValidate()) return new ResponseResult<>(ResponseMessage.PARAMETER_ERROR);
		// 3、发表评论
		EgComment result = commentService.create(comment, uid);
		if(result == null) return new ResponseResult<>(ResponseMessage.CREATE_FAIL);
		return new ResponseResult<>(ResponseMessage.OK, result);
	}

	/**
	 * 修改评论
	 * @param comment
	 * @return
	 * @throws Exception
	 */
	@Token
	@PostMapping("/update")
	public ResponseResult<EgComment> update(@RequestBody EgComment comment) throws Exception {
		// 1、获取当前登录用户Id
		//String token = request.getHeader("Authorization");
		Long uid = (Long)request.getAttribute("userId");
		// 2、判断主键ID
		if(comment.getId() == null || comment.getId() == 0) return new ResponseResult<>(ResponseMessage.NOTNULL_ID);
		// 3、根据主键Id查询评论
		EgComment oldComment = commentService.detail(comment.getId());
		if(oldComment == null) return new ResponseResult<>(ResponseMessage.DATA_NOT_FOUND);
		// 4、判断当前登录用户是否为评论发布者
		if(!uid.equals(oldComment.getUser().getId())) return new ResponseResult<>(ResponseMessage.INADEQUATE_PERMISSIONS);
		// 5、修改评论
		EgComment result = commentService.update(comment);
		if(result == null) return new ResponseResult<>(ResponseMessage.UPDATE_FAIL);
		return new ResponseResult<>(ResponseMessage.OK, result);
	}

	/**
	 * 删除评论
	 * @param cmid
	 * @return
	 * @throws Exception
	 */
	@Token
	@PostMapping("/del")
	public ResponseResult<Long> del(@RequestParam Long cmid) throws Exception {
		// 1、获取当前登录用户Id
		//String token = request.getHeader("Authorization");
		Long uid = (Long)request.getAttribute("userId");
		EgUser user = userService.detail(uid);
		Long result = 0L;
		// 2、当前登录用户拥有信息审核权限，直接删除
		if((user.getRole().getPurview() & EgPurview.OBJECT_CHECK) > 0) {
			result = commentService.del(cmid);
			if(result == null) return new ResponseResult<>(ResponseMessage.DELETE_FAIL);
			return new ResponseResult<>(ResponseMessage.OK, result);
		}
		// 3、根据主键Id查询评论
		EgComment oldComment = commentService.detail(cmid);
		if(oldComment == null) return new ResponseResult<>(ResponseMessage.DATA_NOT_FOUND);
		// 4、判断当前登录用户是否为评论发布者
		if(!uid.equals(oldComment.getUser().getId())) return new ResponseResult<>(ResponseMessage.INADEQUATE_PERMISSIONS);
		// 5、修改评论
		result = commentService.del(cmid);
		if(result == 0) return new ResponseResult<>(ResponseMessage.DELETE_FAIL);
		return new ResponseResult<>(ResponseMessage.OK, result);
	}

	/**
	 * 根据过滤条件查询评论列表
	 * @param filter
	 * @return
	 * @throws Exception
	 */
	@Token
	@PostMapping("/query")
	public ResponseResult<PageInfo<EgComment>> query(@RequestBody OtypeFilter filter) throws Exception {
		return new ResponseResult<>(ResponseMessage.OK, commentService.query(filter));
	}
}
